﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.Data;
using System.ComponentModel;
using mprsc;
using Microsoft.ReportingServices.ReportRendering;


/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage 
{
    public BasePage()
    {
        //
        // TODO: Add constructor logic here
        //
    }
   
    public static string SetShoppingCartSesstion(string ProductCodeAndPrice)
    {
        try
        {
            string strResult = String.Empty;
            if (ProductCodeAndPrice != String.Empty)
            {
                string[] strCodeAndPrice = ProductCodeAndPrice.Split('|');
                
                
                TblBookingDetail order_detail = new TblBookingDetail();

                TblProductsDAO itemsDAO = new TblProductsDAO(Global.CONNECTION_STRING);
                TblProducts item = itemsDAO.GetProductByIDProduct(int.Parse(strCodeAndPrice[0].Trim()));
                
              
                order_detail.IdOrder= 1;
                order_detail.ProductId = item.ProductID;
                order_detail.Items = item;



                order_detail.Quantity = 1;
                order_detail.UnitPrice = item.RRP;
                order_detail.ProductName = item.ProductName;

               

                List<TblBookingDetail> lstItems = null;

                if (Global.SESSION_CART.ToString() == null)
                {
                    lstItems = new List<TblBookingDetail>();

                }
                else
                {
                   // lstItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];
                  
                }

                AddItemToCurrentCart(ref lstItems, order_detail);
                //Session[Global.SESSION_CART] = lstItems;
                strResult = BuildItemsAndAmount(lstItems);
            
            }
            return strResult;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static void AddItemToCurrentCart(ref List<TblBookingDetail> ItemsList, TblBookingDetail order_detail)
    {
        bool bolExist = false;
        try
        {            
            foreach (TblBookingDetail o in ItemsList)
            {
                if (o.ProductId == order_detail.ProductId)
                {
                    o.Quantity++;
                    o.ToatalAmount = o.Quantity * o.UnitPrice;
                    bolExist = true;
                    return;
                }
            }
            if(!bolExist)
                ItemsList.Add(order_detail);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static string BuildItemsAndAmount(List<TblBookingDetail> ItemsList)
    {
        try
        {
            int iQuantity = 0;
            decimal dAmount = 0;
            foreach (TblBookingDetail order in ItemsList)
            {
                iQuantity += order.Quantity;
                dAmount += order.Quantity * order.UnitPrice;
            }

            return iQuantity.ToString() + "|" + dAmount.ToString("c");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
